package com.yzcrm.erp.admin.controller;

import com.jfinal.plugin.activerecord.Record;
import com.yzcrm.common.annotation.Permissions;
import com.yzcrm.erp.admin.entity.AdminDept;
import com.yzcrm.erp.admin.service.AdminDeptService;
import com.yzcrm.utils.BaseUtil;
import com.yzcrm.utils.R;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.paragetter.Para;

/**
 * @author hmb
 */
public class AdminDeptController extends Controller {

    @Inject
    private AdminDeptService adminDeptService;

    /**
     * @param adminDept 部门对象
     * @author hmb
     * 设置部门
     */
    @Permissions("manage:users:deptSave")
    public void addDept(@Para("") AdminDept adminDept) {
        renderJson(adminDeptService.setDept(adminDept));
    }

    @Permissions("manage:users:deptUpdate")
    public void updateDept(@Para("") AdminDept adminDept) {
        renderJson(adminDeptService.setDept(adminDept));
    }

    /**
     * @author hmb
     * 查询部门tree列表
     */
    public void queryDeptTree() {
        String type = getPara("type");
        Integer id = getParaToInt("id");
        renderJson(R.ok().put("data", adminDeptService.queryDeptTree(type, id)));
    }

    /**
     * @author zhangzhiwie
     * 查询权限内部门
     */
    public void queryDeptByAuth() {
        renderJson(R.ok().put("data", adminDeptService.queryDeptByAuth(BaseUtil.getUser().getUserId())));
    }

    /**
     * @author hmb
     * 删除部门
     */
    @Permissions("manage:users:deptDelete")
    public void deleteDept() {
        String id = getPara("id");
        renderJson(adminDeptService.deleteDept(id));
    }

    /**
     * 查询部门负责人
     */
    public void getLeader() {
        Integer deptId = Integer.valueOf(getPara("deptId"));
        Record leader = adminDeptService.getLeader(deptId);
        renderJson(R.ok().put("leader", leader));
    }
}
